package cn.mogugo.util.tools;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import cn.mogugo.comm.PageSupport;


public class Page {

	private Page(){}
	
	public static Page getInstance(){
		return new Page();
	}
	
	
	
	public String createPageStrForJs(HttpServletRequest request , PageSupport apage ){
		int endPageNo = apage.getTotalPageCount();
		String strPageNo = request.getParameter("pageNo");
		int curPageNo = 0;
		
		int[] pageNoForJs = new int[4];
		pageNoForJs[0] = 1;
		pageNoForJs[3] = endPageNo;
		if(StringUtils.isEmpty(strPageNo)){
			curPageNo = 1;
			pageNoForJs[1] = curPageNo;
			if(endPageNo <= 2){
				pageNoForJs[2] = endPageNo;
			}else{
				pageNoForJs[2] = pageNoForJs[1] + 1;
			}
		}else{
			Integer pageNo = NumberTools.formatObject2IntDefaultZeroNoExp(strPageNo);
			if(pageNo <= 1){
				curPageNo = 1;
				pageNoForJs[1] = curPageNo;
				if(endPageNo <= 2){
					pageNoForJs[2] = endPageNo;
				}else{
					pageNoForJs[2] = curPageNo + 1;
				}				
			}else if(pageNo > 1 && pageNo < endPageNo){
				curPageNo = pageNo;
				pageNoForJs[1] = curPageNo -1;
				if(endPageNo <= 2){
					pageNoForJs[2] = endPageNo;
				}else{
					pageNoForJs[2] = curPageNo + 1;
				}
			}else{
				curPageNo = endPageNo;
				pageNoForJs[1] = curPageNo -1;
				pageNoForJs[2] = curPageNo;
			}
		}
		
		
		StringBuffer tmp = new StringBuffer();
		tmp.append("合计：").append(apage.getTotalCount()).append("条&nbsp共").append(endPageNo).append("页&nbsp;&nbsp;")
		   .append("当前是：第").append(curPageNo).append("页&nbsp;&nbsp;&nbsp;&nbsp;")
		   .append("<a href=\"javascript:void(0)\" onclick=\"gotoPage(").append(pageNoForJs[0]).append(")\">首页</a>&nbsp;&nbsp;")
		   .append("<a href=\"javascript:void(0)\" onclick=\"gotoPage(").append(pageNoForJs[1]).append(")\">上页</a>&nbsp;&nbsp;")
		   .append("<a href=\"javascript:void(0)\" onclick=\"gotoPage(").append(pageNoForJs[2]).append(")\">下页</a>&nbsp;&nbsp;")
		   .append("<a href=\"javascript:void(0)\" onclick=\"gotoPage(").append(pageNoForJs[3]).append(")\">末页</a>");
		return tmp.toString();
	}
	
	public String[] createPageNumberStr(HttpServletRequest request , PageSupport apage ){
		String[] strPage = new String[2];
		StringBuffer strbufUrl = new StringBuffer(request.getRequestURI());
		int curPageNo = 1;
		int totalCount = 0;
		int totalPageNo = 0;
		try{
			curPageNo = apage.getStartIndex();
			totalCount = apage.getTotalCount();
			totalPageNo = apage.getTotalPageCount();
		}catch(NullPointerException ne){}
		
		String url = getQueryStrArray(request.getQueryString());
		
		strPage[0] = "合计："+totalCount+"条&nbsp共"+totalPageNo+"页";
		
		
		/*
		
		<li><a href='/list.php?op=search&lm=13&sortby=renqi&page=2' title='下一页家常菜谱大全介绍'>上一页</a></li>
		<li><a class='current' href='#'>1</a></li>
		<li><a href='/list.php?op=search&lm=13&sortby=renqi&page=2'>2</a></li>
		<li><a href='/list.php?op=search&lm=13&sortby=renqi&page=3'>3</a></li>
		<li><a href='/list.php?op=search&lm=13&sortby=renqi&page=4'>4</a></li>
		<li><a href='/list.php?op=search&lm=13&sortby=renqi&page=5'>5</a></li>
		<li><a href='/list.php?op=search&lm=13&sortby=renqi&page=2' title='下一页家常菜谱大全介绍'>下一页</a></li>
		
		*/
		
		StringBuffer strbuf = new StringBuffer();
		if(totalPageNo == 1){
			strbuf.append("<li><a href='javascript:void(0)'>上一页</a></li><li><a class='current' href='javascript:void(0)'>1</a></li><li><a href='javascript:void(0)'>下一页</a></li>");
		}else if(totalPageNo > 1 && totalPageNo <= 7){
			if(curPageNo == 1){
				strbuf.append("<li><a href='javascript:void(0)'>上一页</a></li><li><a class='current' href='javascript:void(0)'>1</a></li>");
				for(int i = 2 ; i <= totalPageNo ; i ++ ){
					strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+"")+"'>"+i+"</a></li>");
				}
				strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "2")+"'>下一页</a></li>");
			}else if(curPageNo == totalPageNo){
				strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo-1+"")+"'>上一页</a></li>");
				for(int i = 1 ; i < totalPageNo ; i ++ ){
					strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+"")+"'>"+i+"</a></li>");
				}
				strbuf.append("<li><a class='current' href='javascript:void(0)'>"+totalPageNo+"</a></li><li><a href='javascript:void(0)'>下一页</a></li>");
			}else{
				strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo-1+"")+"'>上一页</a></li>");
				for(int i = 1 ; i <= totalPageNo ; i ++ ){
					if(i == curPageNo){
						strbuf.append("<li><a class='current' href='javascript:void(0)'>"+i+"</a></li>");
					}else{
						strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+"")+"'>"+i+"</a></li>");
					}
				}
				strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo+1+"")+"'>下一页</a></li>");
			}
		}else{
			if(curPageNo == 1){
				strbuf.append("<li><a href='javascript:void(0)'>上一页</a></li><li><a class='current' href='javascript:void(0)'>1</a></li>");
				for(int i = 2 ; i <= 7 ; i ++ ){
					strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+"")+"'>"+i+"</a></li>");
				}
				strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "2")+"'>下一页</a></li>");
			}else if(curPageNo == totalPageNo){
				strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo-1+"")+"'>上一页</a></li>");
				for(int i = totalPageNo-6 ; i < totalPageNo ; i ++ ){
					strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+"")+"'>"+i+"</a></li>");
				}
				strbuf.append("<li><a class='current' href='javascript:void(0)'>"+totalPageNo+"</a></li><li><a href='javascript:void(0)'>下一页</a></li>");
			}else{
				strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo-1+"")+"'>上一页</a></li>");
				int startPageNo = 0;
				int endPageNo = 0;
				if(curPageNo <= 4){
					startPageNo = 1;
					endPageNo = 7;
				}else if(curPageNo > totalPageNo -4){
					startPageNo = totalPageNo - 6;
					endPageNo = totalPageNo;
				}else{
					startPageNo = curPageNo - 3;
					endPageNo = curPageNo + 3;
				}
				
				for(int i = startPageNo ; i <= endPageNo ; i ++ ){
					if(i == curPageNo){
						strbuf.append("<li><a class='current' href='javascript:void(0)'>"+i+"</a></li>");
					}else{
						strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+"")+"'>"+i+"</a></li>");
					}
				}
				strbuf.append("<li><a href='"+strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo+1+"")+"'>下一页</a></li>");
			}
		}
		strPage[1] = strbuf.toString();
		return strPage;
	}
	
	
	public String[] createPageNumberStrForJoke7(HttpServletRequest request , HttpServletResponse response , PageSupport apage ){
		String[] strPage = new String[2];
		StringBuffer strbufUrl = new StringBuffer(request.getRequestURI());
		int curPageNo = 1;
		int totalCount = 0;
		int totalPageNo = 0;
		try{
			curPageNo = apage.getStartIndex();
			totalCount = apage.getTotalCount();
			totalPageNo = apage.getTotalPageCount();
		}catch(NullPointerException ne){}
		
		String url = getQueryStrArray(request.getQueryString());
		
		
		strPage[0] = "<span class=\"no\">共"+curPageNo+"/"+totalPageNo+"页 "+totalCount+"条</span>";
		
		
		StringBuffer strbuf = new StringBuffer();
		if(totalPageNo == 1){
			strbuf.append("<a href='javascript:void(0)'><<</a><a href='javascript:void(0)'><</a><span class=\"current\"><a href='javascript:void(0)'>1</a></span><a href='javascript:void(0)'>></a><a href='javascript:void(0)'>>></a>");
		}else if(totalPageNo > 1 && totalPageNo <= 7){
			if(curPageNo == 1){
				strbuf.append("<a href='javascript:void(0)'><<</a><a href='javascript:void(0)'><</a><span class=\"current\"><a href='javascript:void(0)'>1</a></span>");
				for(int i = 2 ; i <= totalPageNo ; i ++ ){
					strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+""))+"'>"+i+"</a>");
				}
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "2"))+"'>></a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", totalPageNo+""))+"'>>></a>");
			}else if(curPageNo == totalPageNo){
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "1"))+"'><<</a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo-1+""))+"'><</a>");
				for(int i = 1 ; i < totalPageNo ; i ++ ){
					strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+""))+"'>"+i+"</a>");
				}
				strbuf.append("<span class=\"current\"><a href='javascript:void(0)'>"+totalPageNo+"</a></span><a href='javascript:void(0)'>></a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", totalPageNo+""))+"'>>></a>");
			}else{
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "1"))+"'><<</a>")
				      .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo-1+""))+"'><</a>");
				for(int i = 1 ; i <= totalPageNo ; i ++ ){
					if(i == curPageNo){
						strbuf.append("<span class=\"current\"><a href='javascript:void(0)'>"+i+"</a></span>");
					}else{
						strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+""))+"'>"+i+"</a>");
					}
				}
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo+1+""))+"'>></a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", totalPageNo+""))+"'>>></a>");
			}
		}else{
			if(curPageNo == 1){
				strbuf.append("<a href='javascript:void(0)'><<</a><a href='javascript:void(0)'><</a><span class=\"current\"><a href='javascript:void(0)'>1</a></span>");
				for(int i = 2 ; i <= 7 ; i ++ ){
					String aPageUrl = strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+"");
					strbuf.append("<a href='"+response.encodeURL(aPageUrl)+"'>"+i+"</a>");
				}
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "2"))+"'>></a>")
				      .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", totalPageNo+""))+"'>>></a>");
			}else if(curPageNo == totalPageNo){
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "1"))+"'><<</a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo-1+""))+"'><</a>");
				for(int i = totalPageNo-6 ; i < totalPageNo ; i ++ ){
					strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+""))+"'>"+i+"</a>");
				}
				strbuf.append("<span class=\"current\"><a href='javascript:void(0)'>"+totalPageNo+"</a></span><a href='javascript:void(0)'>></a><a href='javascript:void(0)'>>></a>");
			}else{
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "1"))+"'><<</a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo-1+""))+"'><</a>");
				int startPageNo = 0;
				int endPageNo = 0;
				if(curPageNo <= 4){
					startPageNo = 1;
					endPageNo = 7;
				}else if(curPageNo > totalPageNo -4){
					startPageNo = totalPageNo - 6;
					endPageNo = totalPageNo;
				}else{
					startPageNo = curPageNo - 3;
					endPageNo = curPageNo + 3;
				}
				
				for(int i = startPageNo ; i <= endPageNo ; i ++ ){
					if(i == curPageNo){
						strbuf.append("<span class=\"current\"><a href='javascript:void(0)'>"+i+"</a></span>");
					}else{
						strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+""))+"'>"+i+"</a>");
					}
				}
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo+1+""))+"'>></a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", totalPageNo+""))+"'>>></a>");
			}
		}
		strPage[1] = strbuf.toString();
		return strPage;
	}
	
	
	
	
	public String[] createPageNumberStrForJoke5(HttpServletRequest request , HttpServletResponse response , PageSupport apage ){
		String[] strPage = new String[2];
		StringBuffer strbufUrl = new StringBuffer(request.getRequestURI());
		int curPageNo = 1;
		int totalCount = 0;
		int totalPageNo = 0;
		try{
			curPageNo = apage.getStartIndex();
			totalCount = apage.getTotalCount();
			totalPageNo = apage.getTotalPageCount();
		}catch(NullPointerException ne){}
		
		String url = getQueryStrArray(request.getQueryString());
		
		
		strPage[0] = "<span class=\"no\">共"+curPageNo+"/"+totalPageNo+"页 "+totalCount+"条</span>";
		
		
		StringBuffer strbuf = new StringBuffer();
		if(totalPageNo == 1){
			strbuf.append("<a href='javascript:void(0)'><<</a><a href='javascript:void(0)'><</a><span class=\"current\"><a href='javascript:void(0)'>1</a></span><a href='javascript:void(0)'>></a><a href='javascript:void(0)'>>></a>");
		}else if(totalPageNo > 1 && totalPageNo <= 5){
			if(curPageNo == 1){
				strbuf.append("<a href='javascript:void(0)'><<</a><a href='javascript:void(0)'><</a><span class=\"current\"><a href='javascript:void(0)'>1</a></span>");
				for(int i = 2 ; i <= totalPageNo ; i ++ ){
					strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+""))+"'>"+i+"</a>");
				}
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "2"))+"'>></a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", totalPageNo+""))+"'>>></a>");
			}else if(curPageNo == totalPageNo){
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "1"))+"'><<</a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo-1+""))+"'><</a>");
				for(int i = 1 ; i < totalPageNo ; i ++ ){
					strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+""))+"'>"+i+"</a>");
				}
				strbuf.append("<span class=\"current\"><a href='javascript:void(0)'>"+totalPageNo+"</a></span><a href='javascript:void(0)'>></a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", totalPageNo+""))+"'>>></a>");
			}else{
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "1"))+"'><<</a>")
				      .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo-1+""))+"'><</a>");
				for(int i = 1 ; i <= totalPageNo ; i ++ ){
					if(i == curPageNo){
						strbuf.append("<span class=\"current\"><a href='javascript:void(0)'>"+i+"</a></span>");
					}else{
						strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+""))+"'>"+i+"</a>");
					}
				}
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo+1+""))+"'>></a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", totalPageNo+""))+"'>>></a>");
			}
		}else{
			if(curPageNo == 1){
				strbuf.append("<a href='javascript:void(0)'><<</a><a href='javascript:void(0)'><</a><span class=\"current\"><a href='javascript:void(0)'>1</a></span>");
				for(int i = 2 ; i <= 5 ; i ++ ){
					String aPageUrl = strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+"");
					strbuf.append("<a href='"+response.encodeURL(aPageUrl)+"'>"+i+"</a>");
				}
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "2"))+"'>></a>")
				      .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", totalPageNo+""))+"'>>></a>");
			}else if(curPageNo == totalPageNo){
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "1"))+"'><<</a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo-1+""))+"'><</a>");
				for(int i = totalPageNo-4 ; i < totalPageNo ; i ++ ){
					strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+""))+"'>"+i+"</a>");
				}
				strbuf.append("<span class=\"current\"><a href='javascript:void(0)'>"+totalPageNo+"</a></span><a href='javascript:void(0)'>></a><a href='javascript:void(0)'>>></a>");
			}else{
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "1"))+"'><<</a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo-1+""))+"'><</a>");
				int startPageNo = 0;
				int endPageNo = 0;
				if(curPageNo <= 3){
					startPageNo = 1;
					endPageNo = 5;
				}else if(curPageNo > totalPageNo - 3){
					startPageNo = totalPageNo - 4;
					endPageNo = totalPageNo;
				}else{
					startPageNo = curPageNo - 2;
					endPageNo = curPageNo + 2;
				}
				
				for(int i = startPageNo ; i <= endPageNo ; i ++ ){
					if(i == curPageNo){
						strbuf.append("<span class=\"current\"><a href='javascript:void(0)'>"+i+"</a></span>");
					}else{
						strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", i+""))+"'>"+i+"</a>");
					}
				}
				strbuf.append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", curPageNo+1+""))+"'>></a>")
					  .append("<a href='"+response.encodeURL(strbufUrl.toString() + StringTools.setParameters(url, "pageNo", totalPageNo+""))+"'>>></a>");
			}
		}
		strPage[1] = strbuf.toString();
		return strPage;
	}
	
	
	public String createPageStr(HttpServletRequest request , PageSupport apage ){
		StringBuffer strbufUrl = new StringBuffer(request.getRequestURI());
		
		int curPageNo = 1;
		int totalCount = 0;
		int totalPageNo = 0;
		try{
			curPageNo = apage.getStartIndex();
			totalCount = apage.getTotalCount();
			totalPageNo = apage.getTotalPageCount();
		}catch(NullPointerException ne){}
		
		String url = getQueryStrArray(request.getQueryString());
		
		
		
		String[] indexLink = new String[2];
		String[] endLink = new String[2];
		indexLink[0] = strbufUrl.toString() + StringTools.setParameters(url, "pageNo", "1");
		indexLink[1] = strbufUrl.toString() + StringTools.setParameters(url, "pageNo", (curPageNo - 1)+"");

		endLink[0] = strbufUrl.toString() + StringTools.setParameters(url, "pageNo", (curPageNo + 1)+"");
		endLink[1] = strbufUrl.toString() + StringTools.setParameters(url, "pageNo", totalPageNo+"");
		
		String pagination="合计："+totalCount+"条&nbsp共"+totalPageNo+"页&nbsp;&nbsp;当前是：第"+curPageNo+"页&nbsp;&nbsp;&nbsp;&nbsp;";
		if(curPageNo == 1){
			//当前页为第一页
			if(totalPageNo <=1){
				//总页数也只有一页
				//首页，上一页，下一页，尾页均没有链接
				pagination += pageElementTogether(true , null) + pageElementTogether(false , null);
			}else{
				//总页数不止一页
				//首页，上一页没有链接，下一页，尾页有链接
				pagination += pageElementTogether(true , null ) + pageElementTogether(false , endLink);
			}
		}else if(curPageNo > 1 && curPageNo < totalPageNo){
			//不是第一页也不是最后一页
			pagination += pageElementTogether(true , indexLink ) + pageElementTogether(false , endLink);
		}else if(curPageNo == totalPageNo){
			//最后一页
			pagination += pageElementTogether(true , indexLink ) + pageElementTogether(false , null);
		}
		return pagination;
		

		
	
	}
	/**
	 * 计算分页的显示内容
	 * @param isBeginSign
	 * @param link
	 * @return
	 */
	private String pageElementTogether(boolean isBeginSign , String[] link){
		String strRtn = null;
		if(link == null){
			//没有链接
			if(isBeginSign){
				strRtn = "首页&nbsp;&nbsp;上一页&nbsp;&nbsp;";
			}else{
				strRtn = "下一页&nbsp;&nbsp;尾页";
			}
		}else{
			//有链接
			StringBuffer tmp = new StringBuffer();
			if(isBeginSign){
				tmp.append("<a href=\"")
				   .append(link[0])
				   .append("\">首页</a>&nbsp;&nbsp;<a href=\"")
				   .append(link[1])
				   .append("\">上一页</a>&nbsp;&nbsp;");
			}else{
				tmp.append("<a href=\"")
				   .append(link[0])
				   .append("\">下一页</a>&nbsp;&nbsp;<a href=\"")
				   .append(link[1])
				   .append("\">尾页</a>");
			}
			strRtn = tmp.toString();
		}
		return strRtn;
	}
	
	
	private String getQueryStrArray(String queryString){
		
		
		String strRtn = "";
		
		if(StringUtils.isNotBlank(queryString)){
			String[] str = queryString.split("&");
			strRtn = "?";
			boolean hasPageNo = false;
			for(int i = 0 ; i < str.length ; i ++ ){
				if(str[i].indexOf("pageNo=") == 0){
					String[] temp = str[i].split("=");
					str[i] = temp[0]+"=${pageNo}";
					hasPageNo = true;
				}
				strRtn += str[i]+"&";
			}
			if(!hasPageNo){
				strRtn = strRtn + "pageNo=${pageNo}";
			}else{
				strRtn = strRtn.substring(0, strRtn.length()-1);
			}
		}else{
			strRtn += "?pageNo=${pageNo}";
		}
		return strRtn;
	}
	
	
	public static void main(String[] args){
		Page p = Page.getInstance();
		System.out.println(p.getQueryStrArray(""));
	}
}
